import expireCache from './cacheclass';

export enum Keys {
  User = 'user',
  CurrentUser = 'currentUser',
  Debugging = 'debuggingCache', //进入调试模式
  UserExtra = 'userExtra',
  Orientation = 'organization', // 组织信息
  Codelist = 'codelist',
  CodelistAll = 'codelistall',
  Role = 'role',
}
const keys = [
  Keys.User,
  Keys.CurrentUser,
  Keys.Debugging,
  Keys.UserExtra,
  Keys.Orientation,
  Keys.Codelist,
  Keys.CodelistAll,
  Keys.Role,
];

type Set = <T>(key: string, value: T) => void;
type Get = <T>(key: string) => T;
type Remove = (key?: string | string[]) => void;

interface Cache {
  <T>(key: string, value?: T): void | T;
  get: Get;
  set: Set;
  remove: Remove;
}

type Exporter = {
  [key in Keys]: Cache;
};

const exporter: Exporter = {} as Exporter;

keys.forEach((code) => {
  exporter[code] = expireCache.namespace(code);
});

export default exporter;

export interface CacheD<T> {
  (key: string, value?: T): void | T;
  get: (key: string) => T;
  set: (key: string, value: T) => void;
  remove: Remove;
}
